angular.module('BaseControl')

.directive("downloadfile", ['BaseSocket', 'BaseService', function(BaseSocket, BaseService) {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
            filesize: '=?',
            serverpath: '=?',
            downloadpath: '=?',
            downloadfilename: '=?',
            uploadpath: '=?',
            filename: '=?',
            deletefilename: '=?',
            isdownloadable: '=?',
            isupdatable: '=?',
            returndata: '=?',
            downloadcomplete: '&',
            download: '=?',
        },
        templateUrl:
            'modules/Base/client/views/DownloadFile.html',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function($scope){
            var downloadSize = 0;
            var socket = null;
            BaseSocket.connect(function(skt){
                socket = skt;
                socket.on($scope.filename + "_DownloadSize", function(size) {
                    downloadSize += size;
                    circle(downloadSize / $scope.filesize);
                    // console.log((downloadSize / $scope.filesize * 100).toString() + "%");
                });
                socket.on($scope.filename + "_DownloadComplete", function(IsDownload) {
                    if(IsDownload){
                        circle(1);
                        $scope.IsDownloading = false;
                        $scope.downloadcomplete({
                            Data: $scope.returndata,
                            IsDownload: true
                        });
                    }
                    socket.emit("clientDisconnect");
                });
            });

            $scope.download = function() {
                downloadSize = 0;
                $scope.IsDownloading = true;
                BaseService.downloadFile($scope.serverpath, $scope.downloadpath, $scope.downloadfilename, $scope.uploadpath, $scope.filename, $scope.deletefilename, socket.socket.id, function(data){
                    //下载完成，服务器返回
                });
                //$scope.$digest();
            };

            var circle = function(progress){
                var ctx = $scope.circleElement.getContext("2d");
                ctx.lineWidth = 2;
                ctx.strokeStyle = "#0000ff";
                // ctx.font = '10pt';
                // ctx.fillText("5%", 7, 17);
                ctx.beginPath();
                ctx.arc(13, 13,  11, Math.PI * 1.5, (-0.5 + 2 * progress + 2) * Math.PI, false);
                ctx.stroke();
            };
        }],
        link: function (scope, element, attrs) {
            scope.IsDownloading = false;
            scope.Enabled = true;
            scope.Visible = true;
            scope.circleElement = element[0].children[2].children[0];

            //先画好底色
            var ctx = scope.circleElement.getContext("2d");
            ctx.beginPath();
            ctx.lineWidth = 2;
            ctx.strokeStyle = '#ccc'; 
            ctx.arc(13, 13, 11, 0, Math.PI * 2);
            ctx.stroke();

            scope.$watch(
                function(){
                    return attrs["enabled"];
                },
                function(){
                    if (attrs["enabled"] != undefined && attrs["enabled"].trim().toLocaleLowerCase() == "false") {
                        scope.Enabled = false;
                    }
                    else{
                        scope.Enabled = true;
                    }
                }
            );

            scope.$watch(
                function(){
                    return attrs["visible"]
                },
                function(){
                    if (attrs["visible"] != undefined && attrs["visible"].trim().toLocaleLowerCase() == "false") {
                        scope.Visible = false;
                    }
                    else{
                        scope.Visible = true;
                    }
                }
            );
        }
    }
}]);
